#include "wxscustomeditorproperty.h"
#include <prep.h>
#include <wx/dialog.h>
#include <wx/bitmap.h>
#include <wx/propgrid/propgrid.h>
#include <wx/propgrid/propdev.h>
#include <wx/propgrid/advprops.h>
#include <wx/propgrid/manager.h>

namespace {
  class wxsCustomEditorPropertyPropClass: public wxPGProperty {
    public:
      wxsCustomEditorPropertyPropClass(
        const wxString& label,
        const wxString& name,
        wxsCustomEditorProperty* property,
        wxsPropertyContainer* object ):
        wxPGProperty( label, name ),
        Property( property ),
        Object( object ) {
        SetEditor( wxPG_EDITOR( TextCtrlAndButton ) );
        SetValue( Property->GetStr( Object ) );
      }

      virtual bool OnEvent(
        wxPropertyGrid* propgrid,
        wxWindow* wnd_primary,
        wxEvent& event ) {
        if( event.GetEventType() == wxEVT_COMMAND_BUTTON_CLICKED ) {
          if( Property->ShowEditor( Object ) ) {
            SetValueInEvent( Property->GetStr( Object ) );
            return true;
          }
          return false;
        }
        return wxPGProperty::OnEvent( propgrid, wnd_primary, event );
      }
      virtual wxString GetValueAsString( M_Code_unused int flags = 0 ) const {
        return Property->GetStr( Object );
      }
      wxsCustomEditorProperty* Property;
      wxsPropertyContainer* Object;
  };
}

void wxsCustomEditorProperty::PGCreate( wxsPropertyContainer* Object, wxPropertyGridManager* Grid, wxPGId Parent ) {
  wxPGId PGId = Grid->AppendIn( Parent, new wxsCustomEditorPropertyPropClass( GetPGName(), wxPG_LABEL, this, Object ) );
  if( !CanParseStr() ) {
    Grid->LimitPropertyEditing( PGId );
  }
  PGRegister( Object, Grid, PGId );
}

bool wxsCustomEditorProperty::PGRead( M_Code_unused wxsPropertyContainer* Object, wxPropertyGridManager* Grid, wxPGId PGId, M_Code_unused long Index ) {
  return CanParseStr() && ParseStr( Object, Grid->GetPropertyValue( PGId ).GetString() );
}

bool wxsCustomEditorProperty::PGWrite( M_Code_unused wxsPropertyContainer*  Object, M_Code_unused wxPropertyGridManager* Grid,
                                       M_Code_unused wxPGId PGId, M_Code_unused long Index ) {
  return true;
}
